package com.imau.common.fliter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "BaseFilter",urlPatterns = "/*")
public class BaseFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        //统一请求响应设置
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        HttpServletResponse resp=(HttpServletResponse) response;
        HttpServletRequest req = (HttpServletRequest)request;
        resp.setHeader("Content-Type","text/html; charset=utf-8");

        chain.doFilter(request, response);

        System.out.println();
//
//        resp.setHeader("Access-control-Allow-Origin", req.getHeader("Origin"));
//        resp.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
//        resp.setHeader("Access-Control-Allow-Headers", req.getHeader("Access-Control-Request-Headers"));
//        resp.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token,token");//这里“Access-Token”是我要传到后台的内容key
//        resp.addHeader("Access-Control-Expose-Headers", "*");
//        resp.setHeader("Access-Control-Allow-Credentials", "true");
//        resp.setHeader("Cache-Control", "no-cache");
//        resp.setContentType("application/json; charset=utf-8");
//        resp.setCharacterEncoding("UTF-8");
//        resp.setStatus(HttpServletResponse.SC_OK);


    }
}
